<template>
  <div class="entityLink-container">
    <el-form :inline="true" v-model="filterData" class="search-form">
      <el-form-item label="主体来源：" class="form-input">
        <el-input clearable v-model.trim="filterData.mainSourceName"></el-input>
      </el-form-item>
      <el-form-item label="客体来源：" class="form-input">
        <el-input
          clearable
          v-model.trim="filterData.objectSourceName"
        ></el-input>
      </el-form-item>

      <el-form-item label="状态:" class="status form-input">
        <el-select v-model="filterData.status" clearable>
          <el-option label="已确认" value="0"></el-option>
          <el-option label="待确认" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="searchEntityLink"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <div class="file-table">
      <div class="table-title">
        <span class="table-title-text">实体链接</span>
      </div>
      <el-table :data="tableList" class="km-table" v-loading="tableLoading">
        <el-table-column label="序号" align="center" type="index" width="50">
        </el-table-column>
        <el-table-column
          prop="mainName"
          label="实体链接主体"
          align="center"
          min-width="200"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="mainSourceName"
          label="主体来源"
          align="center"
          min-width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="objectName"
          label="实体链接客体"
          align="center"
          width="200"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="objectSourceName"
          label="客体来源"
          align="center"
          min-width="100"
        ></el-table-column>
        <el-table-column
          prop="confidenct"
          label="置信度（%）"
          min-width="70"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          min-width="50"
          align="center"
        >
          <template slot-scope="scope">
            <span
              class="light"
              :class="scope.row.status === 1 ? `light-yellow` : `light-green`"
            >
              {{ scope.row.status === "1" ? "待确认" : "已确认" }}
            </span>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" width="250">
          <template slot-scope="scope">
            <el-link
              :underline="false"
              type="primary"
              class="form-action"
              @click.native.prevent="proofread(scope.row.id, `view`)"
              >查看</el-link
            >
            <el-link
              v-if="scope.row.status === '1'"
              :underline="false"
              type="primary"
              class="form-action"
              @click.native.prevent="proofread(scope.row.id, `edit`)"
              >校核</el-link
            >
            <el-link
              v-if="scope.row.status === '1'"
              :underline="false"
              type="primary"
              class="form-action"
              @click.native.prevent="onUnlink(scope.row.id)"
              >取消链接</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="km-pagination"
        background
        :small="true"
        layout="total, prev, pager, next,sizes, jumper"
        :total="total"
        :hide-on-single-page="!total"
        :page-size.sync="filterData.page_size"
        :pager-count="7"
        :page-sizes="[10, 20, 30]"
        :current-page="filterData.page_index"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <proofreadDialog
      v-if="showProofreadDialog"
      :id="proofreadId"
      :action="proofreadAction"
      @proofreadDialogOff="proofreadDialogOff"
    ></proofreadDialog>
  </div>
</template>

<script>
import proofreadDialog from "./components/proofread.vue";
export default {
  name: "sourceDataManagement",
  components: {
    proofreadDialog,
  },
  data: () => {
    return {
      filterData: {
        mainSourceName: "", // 主体来源
        objectSourceName: "", // 客体来源
        status: "", // 状态
        page_index: 1, // 页码
        page_size: 10, // 每页条数
      },
      tableList: [], // 文件列表
      tableLoading: false,
      total: 0, // 数据总条数
      dialogVisible: false,
      dialogConfig: {},
      dialogConfigSelect: null,
      submitDialogLoading: false,
      delConfirmVisible: false,
      delConfirmLoading: false,
      delTargetFile: null,
      proofreadId: null,
      proofreadAction: null,
      showProofreadDialog: false,
      unlinkDialogVisible: false,
      unlinkLoading: false,
    };
  },
  created() {
    // 获取实体链接列表
    this.getEntityLinkList(this.filterData);
  },
  methods: {
    getEntityLinkList(params) {
      this.tableLoading = true;
      this.$api.knowledgeGeneration
        .getEntityLinkList(params)
        .then((res) => {
          const { code, message, data } = res.data;
          if (code !== 200) {
            this.$message.warning(message);
            return;
          }
          this.total = data.total;
          this.tableList = data.list;
        })
        .finally(() => (this.tableLoading = false));
    },
    // 搜索
    searchEntityLink() {
      this.filterData.page_index = 1;
      this.getEntityLinkList(this.filterData);
    },
    // 修改当前页码
    handleCurrentChange(page) {
      this.filterData.page_index = page;
      this.getEntityLinkList(this.filterData);
    },
    // 修改每页条数
    handleSizeChange() {
      this.getEntityLinkList(this.filterData);
    },

    // 校核及查看
    proofread(id, action) {
      this.proofreadId = id;
      this.proofreadAction = action;
      this.showProofreadDialog = true;
    },
    proofreadDialogOff(refresh) {
      this.showProofreadDialog = false;
      if (refresh) {
        this.getEntityLinkList(this.filterData);
      }
    },

    // 是否确认取消实体链接？
    // 若取消实体链接，该条数据中的两个实体将作为两个单独实体保存到图谱中。
    // 取消链接
    onUnlink(id) {
      this.$confirm(
        "若取消实体链接，该条数据中的两个实体将作为两个单独实体保存到图谱中。",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          this.unlinkLoading = true;
          let res = await this.$api.knowledgeGeneration.cancelEntityLink({
            entityLinkId: id,
          });
          const { message, code } = res.data;
          if (code == 200) {
            this.$message.success("成功取消实体链接");
            this.getEntityLinkList(this.filterData);
          } else {
            this.$message.error(message);
          }
          this.unlinkLoading = false;
        })
        .catch(() => {
          this.$message.info("已取消");
        });
    },

    unlink(id) {
      this.$api.knowledgeGeneration
        .cancelEntityLink({ entityLinkId: id })
        .then((res) => {
          const { message, code } = res.data;
          if (code == 200) {
            this.$message.success(message);
            this.getEntityLinkList(this.filterData);
          } else {
            this.$message.error(message);
          }
        })
        .catch((err) => {
          console.log(err);
          this.$message.error(err);
        })
        .finally(() => {
          this.unlinkLoading = false;
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.entityLink-container {
  .search-form {
    background: #ffffff;
    border-radius: 4px;
    padding: 16px 32px;
    .el-form-item {
      margin-bottom: 0;
    }

    .form-input {
      margin-right: 32px;
      .el-input,
      .el-select {
        ::v-deep .el-input__inner {
          width: 240px;
          height: 32px;
          background: #ffffff;
          border-radius: 2px;
          border: 1px solid #e0e0e0;
        }
      }
    }
  }
  .file-table {
    margin-top: 16px;
    padding: 16px 32px 64px;
    background: #ffffff;
    border-radius: 4px;
    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      &-text {
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
      }

      margin-bottom: 10px;
    }
    padding-bottom: 20px;
  }
  .form-action {
    padding: 0 10px;
    & + .form-action {
      border-left: 1px solid #dddddd;
    }
  }
}
.light {
  &::before {
    content: "";
    display: inline-table;
    margin-right: 6px;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    position: relative;
    top: -2px;
  }
  &-red::before {
    background: #ee5564;
  }
  &-green::before {
    background: #55c898;
  }
  &-yellow::before {
    background: #ffaa45;
  }
}
</style>
